package SwingView.Quiz;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import JDBC.InstructorAssignmentActivities;
import SwingController.ALDownloadFile;
import SwingController.CmsPresenter;
import SwingModel.CmsAssignmentSubmission;
import SwingModel.CmsQuizSubmission;
import SwingModel.CmsStudent;
import SwingView.Quiz.ALQuizViewResult;

import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JSpinner;

import java.awt.Font;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.border.EmptyBorder;

public class StudentQuizSubmissionPanel extends JPanel{

	private CmsStudent student;
	private CmsQuizSubmission quizSub;
	private JLabel jsGrade;

	public StudentQuizSubmissionPanel(CmsStudent student, CmsQuizSubmission quizSub){
		
		this.student = student;
		this.quizSub = quizSub;
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 270, 0, 0};
		setLayout(gridBagLayout);
		setOpaque(true);
		setBorder(BorderFactory.createLineBorder(Color.WHITE));
		
		JLabel lblStudent = new JLabel("Student: ");
		lblStudent.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		GridBagConstraints gbc_lblStudent = new GridBagConstraints();
		gbc_lblStudent.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_lblStudent.insets = new Insets(10, 15, 5, 5);
		gbc_lblStudent.gridx = 0;
		gbc_lblStudent.gridy = 0;
		add(lblStudent, gbc_lblStudent);
		
		JButton lblName = new JButton(student.getFirstName() + " " + student.getLastName());
		lblName.setForeground(Color.BLUE);
		lblName.setOpaque(false);
		lblName.setBorder(BorderFactory.createEmptyBorder());
		lblName.setCursor(new Cursor(Cursor.HAND_CURSOR));

		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.weightx = 0.5;
		gbc_lblName.anchor = GridBagConstraints.LINE_START;
		gbc_lblName.insets = new Insets(10, 0, 5, 5);
		gbc_lblName.gridx = 1;
		gbc_lblName.gridy = 0;
		add(lblName, gbc_lblName);
		
		JLabel lblGivenPoint = new JLabel("Given Point: ");
		lblGivenPoint.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		GridBagConstraints gbc_lblGivenPoint = new GridBagConstraints();
		gbc_lblGivenPoint.insets = new Insets(10, 0, 5, 5);
		gbc_lblGivenPoint.anchor = GridBagConstraints.LINE_START;
		gbc_lblGivenPoint.gridx = 2;
		gbc_lblGivenPoint.gridy = 0;
		add(lblGivenPoint, gbc_lblGivenPoint);
		
		jsGrade = new JLabel();
		GridBagConstraints gbc_spinner = new GridBagConstraints();
		gbc_spinner.anchor = GridBagConstraints.LAST_LINE_START;
		gbc_spinner.weightx = 0.3;
		gbc_spinner.insets = new Insets(0, 0, 5, 0);
		gbc_spinner.gridx = 3;
		gbc_spinner.gridy = 0;
		add(jsGrade, gbc_spinner);
		
		JPanel jpStatus = new JPanel();
		jpStatus.setOpaque(false);
		FlowLayout fl_jpStatus = (FlowLayout) jpStatus.getLayout();
		fl_jpStatus.setAlignment(FlowLayout.LEFT);
		GridBagConstraints gbc_jpStatus = new GridBagConstraints();
		gbc_jpStatus.gridwidth = 4;
		gbc_jpStatus.insets = new Insets(0, 10, 10, 10);
		gbc_jpStatus.fill = GridBagConstraints.BOTH;
		gbc_jpStatus.gridx = 0;
		gbc_jpStatus.gridy = 1;
		add(jpStatus, gbc_jpStatus);
		
		//Instantiate the fields
		if(quizSub == null){//no quiz submission
			setBackground(new Color(255,195,182));//Red background
			
			JLabel lblNoSubmission = new JLabel("No Submission");
			lblNoSubmission.setFont(new Font("Lucida Grande", Font.BOLD, 15));
			
			jpStatus.add(lblNoSubmission);
			
			jsGrade.setText("" + 0);
			
		}
		else{
			
			if(quizSub.getAnswer() == null){
				setBackground(new Color(255,195,182));//Red background
				
				JLabel lblNoSubmission = new JLabel("No Submission");
				lblNoSubmission.setFont(new Font("Lucida Grande", Font.BOLD, 15));
				
				jpStatus.add(lblNoSubmission);
				
				jsGrade.setText("" + 0);
			}
			else{
				setBackground(new Color(178,240,195));//green background
				
				JLabel lblSubmission = new JLabel("Submitted On: ");
				lblSubmission.setFont(new Font("Lucida Grande", Font.BOLD, 15));
				jpStatus.add(lblSubmission);
				
				JLabel lblDate = new JLabel(CmsPresenter.sdf.format(new Date( quizSub.getSubmittedDate().getTime() ) )+ "          ");
				lblDate.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
				jpStatus.add(lblDate);
				
				
				jsGrade.setText("" + quizSub.getGivenPoint() + "%");
				lblName.addActionListener(new ALQuizViewResult(quizSub.getQuizID(),student.getUsername()));
			}
		}
	}

}
